package com.intco.ait.quotation.api.starter.web;

import com.intco.ait.quotation.api.common.model.BaseResponse;
import com.intco.ait.quotation.api.common.model.SkuVo;
import com.intco.ait.quotation.api.common.model.request.SkuSearch;
import com.intco.ait.quotation.api.infrastructure.service.SkuDataService;
import com.intco.ait.quotation.api.starter.advice.LocalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/sku")
@Slf4j
public class SkuController {

    @Autowired
    private SkuDataService skuDataService;

    @PostMapping("/search")
    public BaseResponse<?> getSkuList(@RequestBody SkuSearch skuSearch) {
        try {
            return BaseResponse.success(skuDataService.search(skuSearch));
        } catch (Exception e) {
            log.error("getSkuList调用失败", e);
            throw new LocalException(500, "查询失败:".concat(e.getMessage()), e);
        }
    }

    public BaseResponse<?> getSkuDetail() {
        return BaseResponse.success();
    }

    @PostMapping("/add")
    public BaseResponse<?> createSku(@RequestBody SkuVo skuVo) {
        try {
            skuDataService.createSku(skuVo);
            return BaseResponse.success();
        } catch (Exception e) {
            log.error("createSku调用失败", e);
            throw new LocalException(500, "新增失败:".concat(e.getMessage()), e);
        }
    }

    @PostMapping("/edit")
    public BaseResponse<?> updateSku(@RequestBody SkuVo skuVo) {
        try {
            skuDataService.update(skuVo);
            return BaseResponse.success();
        } catch (Exception e) {
            log.error("updateSku调用失败", e);
            throw new LocalException(500, "修改失败:".concat(e.getMessage()), e);
        }
    }

}
